﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.AspNetCore.Routing.Template;

namespace Having.Site.Route
{
    /// <summary>
    /// 单项的路由配置(该路由通过action操作)
    /// </summary>
    public class RouteItem
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string name { set; get; }
        /// <summary>
        /// 虚拟的url，参数用大括号包起来，例如 /{a}/{b} 
        /// </summary>
        public string url { set; get; }
        /// <summary>
        /// 完整的url(不含表达式)
        /// </summary>
        public string urlText { get; }
        /// <summary>
        /// url对应的url的内部类,例如{a="\w+",b="\d+"}
        /// </summary>
        public object rex { set; get; }

        Dictionary<string, string> _rexdic { get; }
        public Dictionary<string, string> rexdic { get { return _rexdic; } }

        public RouteTemplate rt { get; }
        /// <summary>
        /// 完整正则表达式
        /// </summary>
        public string regstring = "";

        /// <summary>
        /// 处理的结果
        /// </summary>
        public RequestDelegate action { set; get; }

        public RouteItem(string name,string url,object rex, RequestDelegate action)
        {
            this.action = action;
            this.name = name;
            this.url = url;
            this.rex = rex;
            _rexdic = new Dictionary<string, string>();

            if (rex != null)
            {
                regstring = url;
                var t = rex.GetType();
                var sp = t.GetProperties();
                foreach (var item in sp)
                {
                    _rexdic.Add(item.Name, item.GetValue(rex).ToString());
                    regstring = regstring.Replace("{" + item.Name + "}", "("+item.GetValue(rex).ToString()+")");
                }
                this.urlText = url;
                rt = new RouteTemplate(RoutePatternFactory.Parse(this.url, null, this.rex));
                
            }
            else
            {
                var sps = url.Split(new string[] { "/{" }, StringSplitOptions.RemoveEmptyEntries);
                var spr = new List<string>();
                for (var i = 0; i < sps.Length; i++)
                {
                    var sp = sps[i];
                    if (sp.IndexOf(":") > 0)
                    {
                        var key = sp.Split(':')[0];
                        var value = sp.Substring(key.Length + 1).Replace("regex", "").Replace("^", "").Replace("$", "");
                        value = value.Substring(0, value.Length - 1);
                        _rexdic.Add(key, value);
                        spr.Add("{" + key + "}");
                    }
                    else
                    {
                        
                        spr.Add("" + sp + "");
                        continue;
                    }
                }
                this.urlText = string.Join("/", spr);
                rt = TemplateParser.Parse(this.url);
                regstring = urlText;
                foreach (var item in _rexdic)
                {
                    regstring = regstring.Replace("{" + item.Key + "}", "(" + item.Value+")");
                }
            }

        }

    }
}
